package br.ufc.custom.multiHistogram;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.ufc.parser.Predicate;
import br.ufc.parser.Value;

public class Cell {
	private int ID;
	private int count;
	private int partition;
	private Map<Cell, Integer> edges;
	private Map<String, List<Predicate>> predicates;
	private List<Predicate> predicatesList; 
	
	public Cell(int ID){
		this.ID = ID;
		this.count = 0;
//		this.partition = -1;
		this.partition = ID;
		this.edges = new HashMap<Cell, Integer>();
		this.predicates = new HashMap<String, List<Predicate>>();
		this.predicatesList = new ArrayList<Predicate>();
	}
	public Map<Cell, Integer> getEdges() {
		return edges;
	}
	public int getPartition(){
		return this.partition;
	}
	public void setPartition(int partition){
		this.partition = partition;
	}
	public int getCount(){
		return this.count;
	}
	public void incrementCount(){
		this.count ++;
	}
	public int getID(){
		return this.ID;
	}
	public List<Predicate> getPredicate(String att){
		return predicates.get(att);
	}
	public void addPredicate(String attribute, String operation, Value value){
		Predicate predicate = new Predicate(attribute, operation, value);
		List<Predicate> list = predicates.get(attribute);
		if(list == null){
			list = new ArrayList<Predicate>();
			predicates.put(attribute, list);			
		}
		list.add(predicate);
		predicatesList.add(predicate);
	}
	public String createPredicates(){
		String predicatesStatement = "";
		for(Predicate pred : predicatesList){
			predicatesStatement += pred.getAttribute() + " " + 
									pred.getOperation() + " " +
									pred.getValue().toString() + " AND ";
			
		}
		predicatesStatement = predicatesStatement.substring(0, predicatesStatement.length() - 5);
		return predicatesStatement;
	}
	
	
	public void incrementEdge(Cell cell){
		if(edges.containsKey(cell)){
			int edgesCount = edges.get(cell) + 1;
			edges.put(cell, edgesCount);
		}else{
			edges.put(cell, 1);
		}
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ID;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Cell other = (Cell) obj;
		if (ID != other.ID)
			return false;
		return true;
	}
	
}
